﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Store
{
    public partial class 废料资金分配情况 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {                
                this.NianFen.Text = DateTime.Today.Year.ToString();
                this.YueFen.Text = DateTime.Today.Month.ToString();
                Button_Query_Click(null, null);
            }
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;
      

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string sql = @"
                declare @本期总入库 float
                set @本期总入库=(dbo.分厂本期入库(@年度,@月份,1)+dbo.分厂本期入库(@年度,@月份,2)+dbo.分厂本期入库(@年度,@月份,3)+dbo.分厂本期入库(@年度,@月份,5))
                if(@本期总入库>0)
                begin
                select 1 as ID,@年度 as 年度,@月份 as 月份,'一分厂' as 分厂,dbo.分厂本期入库(@年度,@月份,1) as 本期入库,@本期总入库 as 本期总入库,dbo.分厂本期入库(@年度,@月份,1)/@本期总入库 as 占比,[dbo].[废料本期金额](@年度,@月份) as 本期总金额,[dbo].[废料本期金额](@年度,@月份)*dbo.分厂本期入库(@年度,@月份,1)/@本期总入库 as 资金分配情况
                union 
                select 2 as ID,@年度 as 年度,@月份 as 月份,'二分厂' as 分厂,dbo.分厂本期入库(@年度,@月份,2) as 本期入库,@本期总入库 as 本期总入库,dbo.分厂本期入库(@年度,@月份,2)/@本期总入库 as 占比,[dbo].[废料本期金额](@年度,@月份) as 本期总金额,[dbo].[废料本期金额](@年度,@月份)*dbo.分厂本期入库(@年度,@月份,2)/@本期总入库 as 资金分配情况  
                union 
                select 3 as ID,@年度 as 年度,@月份 as 月份,'三分厂' as 分厂,dbo.分厂本期入库(@年度,@月份,3) as 本期入库,@本期总入库 as 本期总入库,dbo.分厂本期入库(@年度,@月份,3)/@本期总入库 as 占比,[dbo].[废料本期金额](@年度,@月份) as 本期总金额,[dbo].[废料本期金额](@年度,@月份)*dbo.分厂本期入库(@年度,@月份,3)/@本期总入库 as 资金分配情况  
                union 
                select 5 as ID,@年度 as 年度,@月份 as 月份,'五分厂' as 分厂,dbo.分厂本期入库(@年度,@月份,5) as 本期入库,@本期总入库 as 本期总入库,dbo.分厂本期入库(@年度,@月份,5)/@本期总入库 as 占比,[dbo].[废料本期金额](@年度,@月份) as 本期总金额,[dbo].[废料本期金额](@年度,@月份)*dbo.分厂本期入库(@年度,@月份,5)/@本期总入库 as 资金分配情况 
                end";
            sql = sql.Replace("@年度", this.NianFen.Text);
            sql = sql.Replace("@月份", this.YueFen.Text);
            this.SqlDataSource1.SelectCommand = sql;
        }
    }
}
